package org.example.controller;

import org.apache.commons.lang3.StringUtils;
import org.example.entity.EsDocx;
import org.example.exception.BusinessException;
import org.example.service.DocService;
import org.example.vo.FileQuery;
import org.example.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class DocxController {
    @Autowired
    private DocService docService;


    @PostMapping("/list-file")
    public ResultVo<List<EsDocx>> listDox(@RequestBody FileQuery fileQuery) {
        List<EsDocx> list = docService.list(fileQuery);
        return new ResultVo<List<EsDocx>>().success(list);
    }

    @PostMapping("/upload-doc")
    public ResultVo<String> uploadDocx(@RequestParam("docx") MultipartFile docx) {
        if (StringUtils.isNotBlank(docx.getOriginalFilename())
                & (docx.getOriginalFilename().endsWith("docx")
                || docx.getOriginalFilename().endsWith("doc"))) {
            return new ResultVo<String>().success(docService.insertIntoEs(docx));
        }
        throw new BusinessException("only docx or doc file can uploaded!");

    }

    @PostMapping("/delete-docs")
    public ResultVo<Long> deleteDocx(@RequestBody List<String> fileIds) {
        return new ResultVo<Long>().success(docService.deleteFileRecord(fileIds));
    }
}
